package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.decoration.DividerItemDecoration;
import com.eduhzy.ttw.commonsdk.decoration.RecycleViewDivider;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerMineSchoolComponent;
import com.eduhzy.ttw.teacher.mvp.contract.MineSchoolContract;
import com.eduhzy.ttw.teacher.mvp.model.entity.ChildAreaData;
import com.eduhzy.ttw.teacher.mvp.presenter.MineSchoolPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.mcxtzhang.indexlib.IndexBar.widget.IndexBar;
import com.mcxtzhang.indexlib.suspension.SuspensionDecoration;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.Subscriber;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-18 16:29:59
 */
@Route(path = RouterHub.APP_MINESCHOOLACTIVITY)
public class MineSchoolActivity extends BaseActivity<MineSchoolPresenter> implements MineSchoolContract.View {

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.indexBar)
    IndexBar indexBar;
    @BindView(R.id.tvSideBarHint)
    TextView tvSideBarHint;
    @Autowired(name = Constants.ROUTER_STRING)
    String areaCode = "0";
    @Autowired(name = Constants.ROUTER_INTEGER)
    int type = 0;

    @Inject
    BaseQuickAdapter<ChildAreaData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    DividerItemDecoration dividerItemDecoration;
    @Inject
    SuspensionDecoration suspensionDecoration;

    private QMUITipDialog tipDialog;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerMineSchoolComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_mine_school;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        rvList.addItemDecoration(suspensionDecoration);
        rvList.addItemDecoration(new RecycleViewDivider(
                getActivity(), LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.public_color_EFEFEF)));
        indexBar.setmPressedShowTextView(tvSideBarHint)//设置HintTextView
                .setNeedRealIndex(true)//设置需要真实的索引
                .setmLayoutManager((LinearLayoutManager) layoutManager);
        mPresenter.getChildArea(areaCode);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ChildAreaData data = mAdapter.getData().get(position);
            //进入选学校
            if (type == 2) {
                ARouter.getInstance().build(RouterHub.APP_CHOOSESCHOOLACTIVITY)
                        .withString(Intent.EXTRA_TITLE, Constants.CHOOSE_SCHOOL)
                        .withParcelable(Constants.ROUTER_DATA,data)
                        .navigation();
                return;
            }
            ARouter.getInstance().build(RouterHub.APP_MINESCHOOLACTIVITY)
                    .withInt(Constants.ROUTER_INTEGER, type + 1)
                    .withString(Constants.ROUTER_STRING, data.getAreaCode())
                    .withString(Intent.EXTRA_TITLE, Constants.ChooseAreaTitle.getTitle(type + 1))
                    .navigation();

        });
    }

    @Override
    public void showLoading() {
        if (tipDialog == null) {
            tipDialog = RxUtil.showLoading(this, "加载中···");
        }
        tipDialog.show();
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(tipDialog)) {
            tipDialog.dismiss();
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void update(List<ChildAreaData> list) {
        getWindow().getDecorView().postDelayed(() -> {
//            ChildAreaData data = new ChildAreaData();
//            data.setAreaName("SB");
//            data.setBaseIndexTag("↑");
//            list.add(0,data);
//            mAdapter.addData(list);

            mAdapter.setNewData(list);
            mAdapter.notifyDataSetChanged();
            indexBar.setmSourceDatas(list).invalidate();
            suspensionDecoration.setmDatas(list);
        }, 500);
    }
     @Subscriber(tag = RouterHub.APP_MINESCHOOLACTIVITY)
     public void onEvents(Message msg) {
         switch (msg.what) {
             case EventBusTags.CHOOSE_SCHOOL_SUCCESS:
                 finish();
                 break;
                 default:
         }
     }

}
